package com.fadadianqi.logistics.admin.controller;

import com.fadadianqi.commons.BackToURLUtils;
import com.fadadianqi.goods.model.Brand;
import com.fadadianqi.goods.model.Category;
import com.fadadianqi.goods.model.Goods;
import com.fadadianqi.goods.service.GoodsService;
import com.fadadianqi.logistics.inventory.model.Batch;
import com.fadadianqi.logistics.inventory.service.BatchService;
import com.yeqiangwei.commons.util.BeanUtils;
import com.yeqiangwei.commons.util.ParamUtils;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class BatchModifyController {

    private static final String PARAM_BACK_URL = "backURL";

    @RequestMapping(value = "/a/batch/modify", method = RequestMethod.GET)
    public String showForm(HttpServletRequest request, ModelMap modelMap) {

        BackToURLUtils.setParamURLToAttr(request, PARAM_BACK_URL);

        String id = ParamUtils.getString(request, "id", null);

        Batch batch = batchService.getById(id);
        Goods goods = goodsService.getGoodsById(batch.getGoodsId());
        Brand brand = goodsService.getBrandById(batch.getBrandId());
        Category category = goodsService.getCategoryById(batch.getCategoryId());

        modelMap.put("goods", goods);
        modelMap.put("brand", brand);
        modelMap.put("category", category);
        modelMap.put("batch", batch);

        BatchForm form = new BatchForm();
        BeanUtils.copyProperties(form, batch);
        modelMap.put("form", form);

        return "/admin/batch/modify";
    }

    @RequestMapping(value = "/a/batch/modify", method = RequestMethod.POST)
    public String doSave(@ModelAttribute("form") BatchForm form,
                         BindingResult result, SessionStatus status
            , HttpServletRequest request, HttpServletResponse response
            , ModelMap modelMap) {

        Batch batch = batchService.getById(form.getId());
        batch.setMemo(form.getMemo());
        batch.setBrandId(form.getBrandId());
        batch.setCategoryId(form.getCategoryId());
        batch.setGoodsId(form.getGoodsId());
        batch.setCost(form.getCost());
        batch.setDiscountRate(form.getDiscountRate());
        batch.setPurchase(form.getPurchase());
        batch.setTrade(form.getTrade());
        batchService.update(batch);

        String backURL = ParamUtils.getString(request, PARAM_BACK_URL, null);

        if (ValidatorHelper.isNotEmpty(backURL)) {
            return "redirect:" + backURL;
        } else {
            return "redirect:/a/batch/list?cid="
                    + form.getCategoryId()
                    + "&bid=" + form.getBrandId();
        }

    }

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private BatchService batchService;

    public static class BatchForm {

        private String id;
        private String brandId;
        private String goodsId;
        private String categoryId;
        private Double purchase; // 进货价
        private Double discountRate; // 进货折扣比率
        private Double cost; // 成本
        private Double trade; // 批发价
        private String memo; // 备注描述 一般记录成本计算公式


        public Double getTrade() {
            return trade;
        }

        public void setTrade(Double trade) {
            this.trade = trade;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getBrandId() {
            return brandId;
        }

        public void setBrandId(String brandId) {
            this.brandId = brandId;
        }

        public String getGoodsId() {
            return goodsId;
        }

        public void setGoodsId(String goodsId) {
            this.goodsId = goodsId;
        }

        public String getCategoryId() {
            return categoryId;
        }

        public void setCategoryId(String categoryId) {
            this.categoryId = categoryId;
        }

        public Double getPurchase() {
            return purchase;
        }

        public void setPurchase(Double purchase) {
            this.purchase = purchase;
        }

        public Double getDiscountRate() {
            return discountRate;
        }

        public void setDiscountRate(Double discountRate) {
            this.discountRate = discountRate;
        }

        public Double getCost() {
            return cost;
        }

        public void setCost(Double cost) {
            this.cost = cost;
        }

        public String getMemo() {
            return memo;
        }

        public void setMemo(String memo) {
            this.memo = memo;
        }
    }
}
